<?php

/*
    Session class.
    
        Author:         Alfredo Mungo
        License:        LGPL
        Copyright:      2012
*/

class Session {

    const CACHE_PUBLIC =            'public';
    const CACHE_PRIVATE =           'private';
    const CACHE_PRIVATE_NO_EXPIRE = 'private_no_expire';
    const CACHE_NOCACHE =           'nocache';

    const SESSION_FILES =           'files';
    const SESSION_MEMORY =          'mm';
    const SESSION_USER =            'user';

    /*
        Start a new session or resume a previous opened one.
        
        Arguments:
            $name: The session name or null.
        
        Throw:
            UnableToStartSessionException: if the session cannot be started.
    */
    public static function start($name = null) {
        
        if($name !== null)
            session_name($name);
        
        if(!session_start())
            throw new UnableToStartSessionException();
    }
    
    public static function destroy() { return session_destroy(); }
    
    public static function cacheExpire($expire = null) { return ($expire !== null)? session_cache_expire($expire) : session_cache_expire(); }
    public static function cacheLimiter($limiter = null) { return ($limiter !== null)? session_cache_limiter($limiter) : session_cache_limiter(); }
    
    public static function decode($data) { return session_decode($data); }
    public static function encode() { return session_encode(); }
    
    public static function getID($id = null) { return ($id !== null)? session_id($id): session_id(); }
    
    public static function name($name = null) { return ($name === null)? session_name(): session_name($name); }
    
    public static function regenerateID($delete_old_session = false) { return session_regenerate_id($delete_old_session); }
    
    public static function unsetVariables() { session_unset(); }
    
    public static function moduleName($name = null) { return ($name === null)? session_module_name(): session_module_name($name); }
    public static function sessionSavePath($path = null) { return ($path === null)? session_save_path(): session_save_path($path); }
    
    public static function getCookieParams() { return session_get_cookie_params(); }
    public static function setCookieParams($lifetime, $path = null, $domain = null, $secure = false, $httponly = false) {
    
        if($path !== null) {
            if($domain !== null)
                session_set_cookie_params($lifetime, $path, $domain, $secure, $false);
            else
                session_set_cookie_params($lifetime, $path);
        }
            else session_set_cookie_params($lifetime);
    }
    
    public static function status() { return session_status(); }
    public static function commit() { session_write_close(); }
    
    public static function serialize() {
        $result = serialize($_SESSION);
        return $result;
    }
    
    public static function unserialize($obj) {
        $s = unserialize($obj);
        
        foreach($s as $k => $v)
            $_SESSION[$k] = $v;
    }
}

?>